﻿using LDM.Admin.Models.Base;
using LDM.Framework.NormalizeActionResult;
using LDM.Sys.ServiceAPI.Model.DBModel;
using LDM.Sys.ServiceAPI.Model.ViewModel.Request.AuthorityReq;
using LDM.Sys.ServiceAPI.Model.ViewModel.Response.Authority;
using LDM.Sys.ServiceAPI.Model.ViewModel.Response.Menu;
using LDM.Sys.ServiceAPI.Svc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace LDM.Admin.Controllers.Authority
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    public class RoleController : Controller
    {
        private IRoleService _roleService;
        private IAccountService _accountService;
        public RoleController(IRoleService roleService, IAccountService accountService)
        {
            _roleService = roleService;
            _accountService = accountService;
        }


        [AllowAnonymous]
        public IActionResult Index()
        {
            return View();
        }
        #region 基础查询

        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetSysRoles(GetSysRoleReq request)
        {
            var result = await _roleService.GetSysRoles(request);
            return Json(BaseGrid<RoleRes>.Instance(result));
        }

        /// <summary>
        ///  添加角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IActionResult> AddRole(AddSysRoleReq request)
        {
            await _roleService.AddSysRole(request);
            return Json(ResultData.Success());
        }

        /// <summary>
        ///  修改角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IActionResult> EditRole(UpdateSysRoleReq request)
        {
            await _roleService.UpdateSysRole(request);
            return Json(ResultData.Success());
        }

        /// <summary>
        ///  删除角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IActionResult> DelSysRole(DelSysRoleReq request)
        {
            await _roleService.DelSysRole(request);
            return Json(ResultData.Success());
        }
        #endregion

        #region 业务查询

        /// <summary>
        /// 查询所有用户简要信息
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> BriefAccounts()
        {
            var result = await _accountService.BriefAccounts();
            return Json(result);
        }

        /// <summary>
        /// 设置角色下用户
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> SetRoleAccounts(SetRoleAccountsReq request)
        {
            await _roleService.SetRoleAccounts(request);
            return Json(ResultData.Success());
        }
        #endregion

    }
}
